/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.wms.service;

import java.text.DecimalFormat;
import java.util.List;

import com.jeeplus.modules.infor.entity.Goods;
import com.jeeplus.modules.infor.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.wms.entity.Outbound;
import com.jeeplus.modules.wms.mapper.OutboundMapper;
import com.jeeplus.modules.wms.entity.OutStatistics;
import com.jeeplus.modules.wms.mapper.OutStatisticsMapper;
import com.jeeplus.modules.wms.entity.Storehouse;
import com.jeeplus.modules.wms.mapper.StorehouseMapper;

/**
 * 出库单Service
 * @author 李超辉
 * @version 2017-12-12
 */
@Service
@Transactional(readOnly = true)
public class OutboundService extends CrudService<OutboundMapper, Outbound> {

	@Autowired
	private OutStatisticsMapper outStatisticsMapper;
	@Autowired
	private StorehouseMapper storehouseMapper;
	@Autowired
	private GoodsService service;
	
	public Outbound get(String id) {
		Outbound outbound = super.get(id);
		outbound.setOutStatisticsList(outStatisticsMapper.findList(new OutStatistics(outbound)));
		outbound.setStorehouseList(storehouseMapper.findList(new Storehouse(outbound)));
		return outbound;
	}
	
	public List<Outbound> findList(Outbound outbound) {
		return super.findList(outbound);
	}
	
	public Page<Outbound> findPage(Page<Outbound> page, Outbound outbound) {
		return super.findPage(page, outbound);
	}
	
	@Transactional(readOnly = false)
	public void save(Outbound outbound) {
		super.save(outbound);
		for (OutStatistics outStatistics : outbound.getOutStatisticsList()){
			if (outStatistics.getId() == null){
				continue;
			}

			if (OutStatistics.DEL_FLAG_NORMAL.equals(outStatistics.getDelFlag())){
				if (StringUtils.isBlank(outStatistics.getId())){
					outStatistics.setOutbound(outbound);
					outStatistics.setType(outbound.getWorkType());
					outStatistics.setPro(outbound.getProjectName());
					outStatistics.preInsert();
					outStatisticsMapper.insert(outStatistics);
				}else{
					outStatistics.preUpdate();
					outStatisticsMapper.update(outStatistics);
				}
			}else{
				outStatisticsMapper.delete(outStatistics);
			}
		}
		for (Storehouse storehouse : outbound.getStorehouseList()){
			if (storehouse.getId() == null){
				continue;
			}
			if (Storehouse.DEL_FLAG_NORMAL.equals(storehouse.getDelFlag())){
				if (StringUtils.isBlank(storehouse.getId())){
					storehouse.setOutbound(outbound);
					storehouse.setStatus("未捡货");
					storehouse.preInsert();
					storehouseMapper.insert(storehouse);
				}else{
					storehouse.preUpdate();
					storehouseMapper.update(storehouse);
				}
			}else{
				storehouseMapper.delete(storehouse);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Outbound outbound) {
		super.delete(outbound);
		outStatisticsMapper.delete(new OutStatistics(outbound));
		storehouseMapper.delete(new Storehouse(outbound));
	}
	
}